﻿using System;
using System.Collections.Generic;
using System.Linq;
using ExpenseSystem.Common;
using ExpenseSystem.Fether;
using ExpenseSystem.Model;
using ExpenseSystem.Lor;

namespace ExpenseSystem.Fether
{
    public class QuoteFetcher : NormalFetcher<QuoteModel>
    {
        public QuoteModel GetByCode( long site, string code, out string message)
        {
            IList<QuoteModel> result = List( site, OrderBy.GetDefault(), Limit.GetDefault(), out message,out rows, Restrain.Eq("Code", code).And(Restrain.Eq("Avaliable", true)));
            if (result.Count > 0)
                return result.First();
            return null;
        }
        public QuoteModel GetGlobal(long site, out string message)
        {
            IList<QuoteModel> result = List( site, OrderBy.GetDefault(), Limit.GetDefault(), out message,out rows, Restrain.Eq("Global", true).And(Restrain.Eq("Avaliable", true)));
            if (result.Count > 0)
                return result.First();
            return null;
        }
        public IList<QuoteModel> ListByTitle(long site, long title,out string message, out int rows)
        {
            TitleQuoteFetcher titleQuoteFetcher = new TitleQuoteFetcher();
            IList<TitleQuoteModel> tqModels = titleQuoteFetcher.ListByTitle(site,title,out message, out rows);
            QuoteFetcher quoteFetcher = new QuoteFetcher();
            Restrain restrain;
            if (tqModels.Count == 0)
                restrain = Restrain.Eq("AutoId", -1);
            else
            {
                IEnumerable<long> titleIds = from item in tqModels select item.QuoteId;
                restrain = Restrain.In<long>("AutoId", titleIds);
            }
            return quoteFetcher.List(site, OrderBy.GetDefault(), Limit.GetDefault(), out message, out rows, restrain);
        }
        public IList<QuoteModel> ListByRegion(long site, long region, out string message,out int rows)
        {
            RegionQuoteFetcher regionQuoteFetcher = new RegionQuoteFetcher();
            IList<RegionQuoteModel> tqModels = regionQuoteFetcher.ListByRegion(site, region, out message,out rows);
            QuoteFetcher quoteFetcher = new QuoteFetcher();
            Restrain restrain;
            if (tqModels.Count == 0)
                restrain = Restrain.Eq("AutoId", -1);
            else
            {
                IEnumerable<long> regionIds = from item in tqModels select item.QuoteId;
                restrain = Restrain.In<long>("AutoId", regionIds);
            }
            return quoteFetcher.List(site, OrderBy.GetDefault(), Limit.GetDefault(), out message,out rows, restrain);
        }
        public IList<QuoteModel> ListBySalesPoint(long site, long salesPoint, out string message,out int rows)
        {
            SalesPointQuoteFetcher salesPointQuoteFetcher = new SalesPointQuoteFetcher();
            IList<SalesPointQuoteModel> tqModels = salesPointQuoteFetcher.ListBySalesPoint(site, salesPoint, out message,out rows);
            QuoteFetcher quoteFetcher = new QuoteFetcher();
            Restrain restrain;
            if (tqModels.Count == 0)
                restrain = Restrain.Eq("AutoId", -1);
            else
            {
                IEnumerable<long> salesPointIds = from item in tqModels select item.QuoteId;
                restrain = Restrain.In<long>("AutoId", salesPointIds);
            }
            return quoteFetcher.List(site, OrderBy.GetDefault(), Limit.GetDefault(), out message, out rows, restrain);
        }
    }
}
